function spike_calibrate()
% function to call the spike_sum routine for calibration purposes
% this is a test routine. It generates an artificial spike waveform
% consisting of triangular waveforms with known rising dv/dt, half-widths, etc.
% Reports the results and the expected results...
% 12/8/98. P. Manis
%

% generate the waveform first
npts = 2048; % total points in the waveform
tdel = 10;
tdur = 100;
rate = 0.1; % data sampling rate
sw1 = floor(tdel/rate); sw2 = floor((tdel+tdur)/rate); % window

rmp = -60;
ap_ht = 70;
spike_thresh = 0; % spike threshold

% time base
tmax = (npts-1)*rate;
time = 0:rate:tmax;

% compute a triangluar AP.
% AP has width of 1 msec
ap_total_width = 2; % total width of the AP
ap_sym  = 0; % skew for ap  0 => symmertical
t_tri = -ap_total_width:rate:ap_total_width; % make template triangle( although I don't use it here)
ap_tri = triangle_pulse(t_tri, ap_total_width, ap_sym);

% make pulse trains
d1=[2 22 34]+tdel;
d2=[4 12.1 15]+tdel;
voltage(1,:) = pulse_train(time,d1,'triangle_pulse', ap_total_width, ap_sym).*ap_ht+rmp;
voltage(2,:) = pulse_train(time,d2,'triangle_pulse', ap_total_width, ap_sym).*ap_ht+rmp;

%plot(time,voltage,'r')



%---------------DO NOT MOFIDY BETWEEN MARKS --------------
% - The following section is extracted from CC_IV

[first_spike, first_isi, nr_spikes, spike_train]=find_spikes(voltage(:,sw1:sw2), rate, spike_thresh);
first_isi=max(first_isi,0);

% make lists that include only spikes during the stimulus window

% for spike times:
%mcurfsl = mean_cur(first_spike>0);
fsl = first_spike(first_spike>0);
%mcurfisi = mean_cur(first_isi>0);
fisi = first_isi(first_isi>0);
v = size(spike_train)
ISILAT=[]; % initialize the ISILAT structure
for i=1:v(2)
   ISILAT(i).nspikes = length(spike_train(i).latency);
   ISILAT(i).isi= diff(spike_train(i).latency);
   ISILAT(i).lat = spike_train(i).latency;
   ISILAT(i).source = spike_train(i).source; % save the source trace number too
   ISILAT(i)
end

% analyze the spikes - hw, dv/dt, amplitude from rest, etc.
nspike = size(ISILAT);
if(nspike(2) > 0)
   spikesumm = spike_sum(voltage, rate, ISILAT, tdel, rmp, 'spike_test', 1, 0);
   
   % summary spike information:
   if(~isempty(spikesumm))
      n = size(spikesumm.vmax);
      spike.n = n(2);
      spike.max=mean(spikesumm.vmax); spike.max_sd = std(spikesumm.vmax);
      
      spike.dvmax = mean(spikesumm.dvmax); spike.dvmax_sd = std(spikesumm.dvmax);
      spike.dvmin = mean(spikesumm.dvmin); spike.dvmin_sd = std(spikesumm.dvmin);
      spike.hwa = mean(spikesumm.hwa); spike.hwa_sd = std(spikesumm.hwa);
      spike.hwb = mean(spikesumm.hwb); spike.hwb_sd = std(spikesumm.hwb);
      spike.ahpdepth = mean(spikesumm.ahpdepth); spike.ahpdepth_sd = std(spikesumm.ahpdepth);
      % spike
      %     CONTROL(sf).spike=spike;
   end
end
%-------------- END OF SECTION "DO NOT MODIFY" -----------------------

spike
